package logic

import (
	"context"
	"errors"
	"shuqi-novel/models"

	"shuqi-novel/novelrpc/internal/svc"
	"shuqi-novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookAddLogic {
	return &BookAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说添加
func (l *BookAddLogic) BookAdd(in *novelrpc.BookAddRequest) (*novelrpc.BookAddResponse, error) {
	// todo: add your logic here and delete this line

	var book models.Book
	models.DB.Debug().Where("title = ?", in.Title).Find(&book)
	if book.ID != 0 {
		return nil, errors.New("当前小说已存在")
	}

	book = models.Book{
		Title:  in.Title,
		Image:  in.Image,
		Desc:   in.Desc,
		UserId: in.UserId,
	}
	models.DB.Debug().Create(&book)

	return &novelrpc.BookAddResponse{
		BookId: int64(book.ID),
	}, nil
}
